package com.huwaiwai.fork_join.demo02;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created by huweiwei on 2017/4/18.
 */
public class Test {

    @org.junit.Test
    public void test01() throws Exception {
        Cache<String, String> cache = CacheBuilder.newBuilder().build();
        for (int i = 0; i < 5; i++) {
            String a = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return cache.get("1", () -> {
                        System.out.println(Thread.currentThread().getName() + "进入");
                        Thread.sleep(5000);
                        return "a";
                    });
                }
            }.call();
            System.out.println(a);
        }
    }

    @org.junit.Test
    public void test02() throws Exception {
        IMyCache cache = new MyCache();
        for (int i = 0; i < 5; i++) {
            Object obj = new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    return cache.get("a", () -> {
                        Thread.sleep(5000);
                        return "1";
                    });
                }
            }.call();
            System.out.println(obj);
        }
    }

    @org.junit.Test
    public void test03() throws ExecutionException, InterruptedException {
        FutureTask<String> future = new FutureTask<>(() -> "1");
        System.out.println(future.get());
    }
}
